var getCookie = readCookie("user")
var myText=decodeURIComponent(location.search)
var group=new Object();
group.tId=""
group.activity_prices=""
group.price=""
group.packData=""
$(function(){
	setVue()
	typeData()
	packData()
	if(myText){
		initData()
	}
	
})

function setVue(){
	vm=new Vue({
		el:".myContent",
		data:{
			cinfo:[],
			tinfo:[],
			pinfo:[]
		},
		methods:{
			orderby(id){
				initData(id,"")
				},
			getType(id){
				typeData(id)
				
			},
			getPack(id){
				packData(id)
			},
			getPc(a,b){
				priceData(a,b)
			},
			addCollect(cId){
				if(getCookie!=""){
					addCollect(cId)
				}else{
				
					location.href="login.htm"
				}
				
			}
			
			}
			
		
	})
}
function priceData(a,b){

	$("#pc150").css("color","#000000")
	$("#pc500").css("color","#000000")
	$("#pc0").css("color","#000000")
	$("#pc50").css("color","#000000")	
	$("#pc"+a).css("color","red") 
	
	initData("",group)
	
}
function packData(id){
	$.ajax({
		url:"http://localhost:8080/pack/select",
		type:"get",
		dataType:"json",
		async:false,
		success:function(response){
	
		for(var i=0;i<response.data.length;i++){
		$("#p"+response.data[i].p_id).css("color","#000000")
		}
		if(id){
			$("#p"+id).css("color","red") 
		}
		
		vm.$data.pinfo=response.data
	
		initData("",group)
			
		}
	})

}
function typeData(id){
		$.ajax({
		url:"http://localhost:8080/brand/select",
		type:"get",
		dataType:"json",
		async:false,
		success:function(response){
			
			
			for(var i=0;i<response.data.length;i++){
			
				$("#t"+response.data[i].b_id).css("color","#000000")
			}
			if(id){
				$("#t"+id).css("color","red")
			}
			vm.$data.tinfo=response.data
			
			initData("",group)
			
		}
	})
	
	}
function getPrice(){
	if($("#pc0").css("color")=="rgb(255, 0, 0)"){
		group.activity_prices=0;
		group.price=50;
	}else if($("#pc50").css("color")=="rgb(255, 0, 0)"){
		group.activity_prices=50;
		group.price=150;
	}else if($("#pc150").css("color")=="rgb(255, 0, 0)"){
		group.activity_prices=150;
		group.price=500;
	}else if($("#pc500").css("color")=="rgb(255, 0, 0)"){
		group.activity_prices=500;
		group.price=1000;
	}

	}
function getPackData(){
	$.ajax({
		url:"http://localhost:8080/pack/select",
		type:"get",
		dataType:"json",
		async:false,
		success:function(response){
			
			for(var i=0;i<response.data.length;i++){
			var getColor=$("#p"+response.data[i].p_id).css("color")
			
			if(getColor=="rgb(255, 0, 0)"){
				
				group.packData=response.data[i].p_id
			}
			}
			
		}
	})

}
function getTypeData(){
	$.ajax({
		url:"http://localhost:8080/brand/select",
		type:"get",
		dataType:"json",
		async:false,
		success:function(response){
			
			for(var i=0;i<response.data.length;i++){
				
				if($("#t"+response.data[i].b_id).css("color")=="rgb(255, 0, 0)"){
					group.tId=response.data[i].b_id
				}
			}
			
		}
	})

}

function initData(id,myGroup){
		getPackData()
		getTypeData()
		getPrice()
		$.ajax({
		url:"http://localhost:8080/commodity/chooseCommodity"+myText+"&brand="+group.tId+"&activity_prices="+group.activity_prices+"&price="+group.price+"&specs="+group.packData,
		type:"get",
		async:false,
		dataType:"json",
		success:function(data){
			if(id==1){
				var arr=data.data
				arr.sort(function(a,b){
					if(a.activity_prices>b.activity_prices){
					return 1;	
					}else if(a.activity_prices<b.activity_prices){
						return -1;
					}else{
						return 0;
					}
				})
				vm.$data.cinfo=arr
			}else if(id==0){
				var arr=data.data
				arr.sort(function(a,b){
					if(a.activity_prices<b.activity_prices){
					return 1;	
					}else if(a.activity_prices>b.activity_prices){
						return -1;
					}else{
						return 0;
					}
				})
				vm.$data.cinfo=arr
			}else if(id==2){
				var arr=data.data
				arr.sort(function(a,b){
					if(a.sales<b.sales){
					return 1;	
					}else if(a.sales>b.sales){
						return -1;
					}else{
						return 0;
					}
				})
				vm.$data.cinfo=arr
			}
			vm.$data.cinfo=data.data
	
		}
	})
	
	
	
}

function addCollect(cId){
	$.ajax({
		url:"http://localhost:8080/collect/add",
		data:{
			"cId":cId,
			"uId":getCookie
		},
		type:"post",
		dataType:"json",
		
		success:function(data){
			
			if(data.status==1){
				$.DialogByZ.Autofade({Content: "收藏完成"})
			}else{
					$.DialogByZ.Alert({Title: "提示", Content: data.msg,BtnL:"确定"})
			}
			
		}
	})
}

